"""
requests学习.py
第三方 发送 http请求的 模块     lib3
pip  install requests
"""
import hashlib
import time

import requests

# 创建一个全局会话对象
sess = requests.session()

# 配置 接口信息
IP = "192.168.1.131"
PORT = "8080"
BASE_URL = "http://"+IP+":"+PORT
class CommonApi:
    # 1.request基础 获取网页信息
    @staticmethod
    def demo1():
        # 发起http请求   method 请求方式   url 资源地址
        # rp = sess.request(method="get",url="http://www.baidu.com")
        url = BASE_URL+"/pinter"
        # print(url)
        rp = sess.request(method="get",url=url)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        print(rp.headers)

    @staticmethod
    # 1.   通用发送get请求接口   params传参
    def demo2():
        # 发起http请求   method 请求方式   url 资源地址
        api_url = "/pinter/com/getSku"
        url = BASE_URL + api_url
        # params 接收get请求参数
        params = {"id":1}
        # print(url)
        rp = sess.request(method="get", url=url,params=params)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        # json 会返回一个 字典
        rp1 = rp.json()
        print(rp1["message"])

    @staticmethod
    # 2.   通用发送post请求接口  data 参数
    def demo3(username,password,apiurl="/pinter/com/login"):
        # 发起http请求   method 请求方式   url 资源地址
        api_url = apiurl
        url = BASE_URL + api_url
        # params 接收get请求参数
        data = {"userName":username,"password":password}
        # print(url)
        rp = sess.request(method="post", url=url,data=data)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        # json 会返回一个 字典
        rp1 = rp.json()
        print(rp1["message"])
        return rp1["message"]

    @staticmethod
    # 3.   通用发送post请求json接口  json 参数
    def demo4(username,password,gender,phonenum,email,address,apiurl="/pinter/com/register"):
        # 发起http请求   method 请求方式   url 资源地址
        api_url = apiurl
        url = BASE_URL + api_url
        # params 接收get请求参数
        json = {"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"beihe@163.com","address":"Beijing"}
        # print(url)
        rp = sess.request(method="post", url=url,json=json)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        # json 会返回一个 字典
        rp1 = rp.json()
        print(rp1["message"])
        return rp1["message"]

    #4.   通用发送post请求  k = v   data 参数
    @staticmethod
    def demo5(apiurl="/pinter/com/buy"):
        # 发起http请求   method 请求方式   url 资源地址
        api_url = apiurl
        url = BASE_URL + api_url
        # params 接收get请求参数
        data = {"param":{"skuId":123,"num":10}}
        # print(url)
        rp = sess.request(method="post", url=url,data=data)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        # json 会返回一个 字典
        rp1 = rp.json()
        print(rp1["message"])
    @staticmethod
    def signapi():

        num = "13899997777"
        optcode = "testfan"
        # 时间戳
        mytime = int(time.time()*1000) # 生成合法的时间戳
        mystr = num + optcode + str(mytime)
        # 加密数据
        sign = hashlib.md5(mystr.encode())


        api_url = "/pinter/com/userInfo"
        url = BASE_URL + api_url
        # json 接收json请求参数
        json = {"phoneNum":num,"optCode":optcode,"timestamp":mytime,"sign":sign.hexdigest()}
        # print(url)
        rp = sess.request(method="post", url=url, json=json)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        # json 会返回一个 字典
        rp1 = rp.json()






class RestfulApi:
    @staticmethod
    def get_api(apiurl="/pinter/com/phone/",id=1):
        # 发起http请求   method 请求方式   url 资源地址
        api_url = apiurl+str(id)
        url = BASE_URL + api_url
        # params 接收get请求参数
        print(url)
        # print(url)
        rp = sess.request(method="get", url=url)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        # json 会返回一个 字典
        rp1 = rp.json()
        print(rp1["message"])

    @staticmethod
    def post_api(apiurl="/pinter/com/phone"):
        # 发起http请求   method 请求方式   url 资源地址
        api_url = apiurl
        url = BASE_URL + api_url
        # json 接收get请求参数
        json = {"brand":"Huawei","color":"yellow","memorySize":"64G","cpuCore":"8核","price":"8848","desc":"全新上市"}
        # print(url)
        rp = sess.request(method="post", url=url,json=json)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        # json 会返回一个 字典
        rp1 = rp.json()
        print(rp1["message"])

class CookieAndToken:
    @staticmethod
    def cookie_api():
        # 1. 先登录
        login_api_url = "/pinter/bank/api/login"
        url = BASE_URL + login_api_url
        # json 接收get请求参数
        data = {"userName":"admin","password":"123456"}
        # print(url)
        rp = sess.request(method="post", url=url, data=data)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)


        # 2. 查询余额
        query_api_url = "/pinter/bank/api/query"
        url = BASE_URL + query_api_url

        params={"userName":"admin"}

        rp = sess.request(method="get",url=url,params=params)
        print(rp.text)

    @staticmethod
    def token_api():
        # 1. 先登录
        login_api_url = "/pinter/bank/api/login2"
        url = BASE_URL + login_api_url
        # json 接收get请求参数
        data = {"userName":"admin","password":"123456"}
        # print(url)
        rp = sess.request(method="post", url=url, data=data)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        rp = rp.json()
        testfan_token = rp["data"]
        # 2. 查询余额
        query_api_url = "/pinter/bank/api/query2"
        url = BASE_URL + query_api_url

        params={"userName":"admin"}
        # 请求头配置 headers
        headers = {"testfan-token": testfan_token}
        rp = sess.request(method="get",url=url,params=params ,headers=headers)
        print(rp.text)

class FileANDSafe:
    @staticmethod
    def fileup():
        login_api_url = "/pinter/file/api/upload2"
        url = BASE_URL + login_api_url
        # 打开文件，
        path = r"d:\log1.txt"
        with open(path,"rb") as f:
            # name  变量的 值  。
            files = {"file":(path, f ,"text/plain")}  #
            rp = sess.request("post",url=url,files=files)
            print(rp.text)
    @staticmethod
    def filedownload():
        filedownload_api_url = "/pinter/file/api/download"
        url = BASE_URL + filedownload_api_url
        # print(url)
        params ={"id":2}
        rp = sess.request(method="get", url=url,params=params)
        # 获取 响应体的原始数据  text    , json
        print(rp.content)





if __name__ == '__main__':
    CommonApi.demo4()
    # RestfulApi.post_api()
    # CookieAndToken.cookie_api()
    # CookieAndToken.token_api()
    # FileANDSafe.fileup()
    # FileANDSafe.filedownload()
    # CommonApi.signapi()
